package LeetCode.month01.day28;

public class No2953 {
    public static void main(String[] args) {
        System.out.println(countCompleteSubstrings("gvgvvgv",2));
    }
    public static int countCompleteSubstrings(String word, int k) {
        char[] c = word.toCharArray();
        int n = word.length();
        int res = 0;
        for (int i = 0; i < n;) {
            int st = i;
            for (i++; i < n&&Math.abs(c[i]-c[i-1])<=2; i++);
            res+=f(word.substring(st,i),k);
        }
        return res;
    }

    public static int f(String s,int k){
        char[] c = s.toCharArray();
        int n = c.length;
        int res = 0;
        for (int i = 1; i<=26&&i*k<=n; i++) {
            int[] cnt = new int[26];
            for (int j = 0; j < n; j++) {
                int index  = c[j]-'a';
                cnt[index]++;
                int left = j-i*k+1;
                if(left>=0){
                    boolean flag = true;
                    for (int i1 : cnt) {
                        if(i1>0&&i1!=k) {
                            flag=false;
                            break;
                        }
                    }
                    if(flag) res++;
                    cnt[c[left]-'a']--;
                }
            }
        }
        return res;
    }
}
